// pages/login/login.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hint: '请输入手机号',
    result: '',
    codeResult: '',
    codeHint: '请输入验证码',
    verifyCodeTime: '获取验证码',
    buttonDisable: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //事件函数
  /* 手机号输入框 */
  bindInputBlur: function (e) {
    var that = this;
    console.log(e.detail.value);
    var PhoneReg = new RegExp('^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$');
    var phones = PhoneReg.exec(e.detail.value);
    
    if (phones) {
      that.setData({
        result: e.detail.value,
        hint: '请输入手机号'
      })
    } else {
      that.setData({
        result: '',
        hint: '手机号有误，请重新输入'
      })
    }
  },
  //获取验证码
  resetCode: function (e){
    var that = this;
    if (this.data.buttonDisable) return false;
    var that = this;
    var c = 88;
    var intervalId = setInterval(function () {
      c = c - 1;
      that.setData({
        verifyCodeTime: c + 's后重发',
        buttonDisable: true
      })
      if (c == 0) {
        clearInterval(intervalId);
        that.setData({
          verifyCodeTime: '获取验证码',
          buttonDisable: false
        })
      }
    }, 1000)
   
      var phones = that.data.result;
      wx.showToast({
        title: phones,
      })

      wx.request({
        url: 'https://xcx.jiagangwangluo.com/api/api/' + 'user/getPhoneCode',
        method: 'get',
        header: {
          'content-type': 'application/json;charset=UTF-8'
        },
        data: {
          phone: phones,
          type: 1
        },
        success: function (res) {
          console.log(res);
        }
      })

      // wx.request({
      //   url: '',
      //   method: "post",
      //   params: {
      //     'client_phone': client_phone,
      //   },
      //   success: function (json) { },
      //   fail: function (json) { }
      // })
    
   
  },
  bindVeriCode: function (e) {
    var that = this;
    that.setData({
      codeResult: e.detail.value,
    })
  },
  formSubmit: function (e) {
   
      console.log('form发生了submit事件，携带数据为：', e.detail.value)
      const client_phone = e.detail.value['clientPhone'];
      const veri_code = e.detail.value['veriCode'];
      var unionId = wx.getStorage('unionId')
      //  wx.getStorage({
      //   key: 'unionId',
      //   success: function (res) {
      //    unionId=res.data;
      //   }
      // })
      wx.request({
        url: 'https://xcx.jiagangwangluo.com/api/api/' + '/user/bindPhone',
        method: "POST",
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          unionid: unionId,
          password:"123456",
          phone: client_phone,
          code: veri_code
        },
        success: function (json) { 
          console.log(json)
        },
        fail: function (json) {

         }
      })
  
  },
  getData: function (params, cb) {
    wx.request({
      url: params._url,
      data: params.data,
      method: 'GET',
      header: {
        'content-type': 'application/json;charset=UTF-8'
      },
      success: function (res) {
        if (res.statusCode == 200) {
          typeof cb === 'function' && cb(res.data)
        }
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  }
  
})